﻿using System;
using System.Net;

namespace com.hetaoos.ForumSpider.Utils
{
    /// <summary>
    /// 能保存 Cookies 的 WebClient
    /// </summary>
    public class HttpClient : WebClient
    {
        // Cookie 容器
        private CookieContainer cookieContainer;
        // 是否支持自动跳转
        private bool allowAutoRedirect = true;

        /// <summary>
        /// 创建一个新的 WebClient 实例。
        /// </summary>
        public HttpClient()
        {
            this.cookieContainer = new CookieContainer();
            Timeout = 300000;
            Proxy = null;
            Encoding = System.Text.Encoding.UTF8;
        }

        // <summary>
        /// 创建一个新的 WebClient 实例。
        /// </summary>
        /// <param name="cookie">Cookie 容器</param>
        public HttpClient(CookieContainer cookies)
        {
            this.cookieContainer = cookies;
            Proxy = null;
        }

        /// <summary>
        /// 使用系统代理
        /// </summary>
        public bool UseSystemWebProxy
        {
            get
            {
                return Proxy != null;
            }
            set
            {
                if (value)
                {
                    Proxy = WebRequest.GetSystemWebProxy();
                }
                else
                {
                    Proxy = null;
                }
            }
        }
        /// <summary>
        /// 页面是否允许自动跳转，302代码
        /// </summary>
        public bool AllowAutoRedirect
        {
            set { allowAutoRedirect = value; }
            get { return allowAutoRedirect; }
        }

        /// <summary>
        /// Cookie 容器
        /// </summary>
        public CookieContainer Cookies
        {
            get { return this.cookieContainer; }
            set { this.cookieContainer = value; }
        }

        public int Timeout { get; set; }

        /// <summary>
        /// 返回带有 Cookie 的 HttpWebRequest。
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.AllowAutoRedirect = allowAutoRedirect;
                httpRequest.CookieContainer = cookieContainer;
                if (Timeout > 0)
                {
                    httpRequest.Timeout = Timeout;
                }
                httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)"; //"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729; .NET4.0E)";
                httpRequest.ContentType = "application/x-www-form-urlencoded";
                httpRequest.Headers["Accept-Encoding"] = "gzip, deflate";
                //httpRequest.KeepAlive = false;

            }
            return request;
        }

        private DateTime lastUseDateTime = new DateTime();
        private bool isLock;
        /// <summary>
        /// 是否已经锁定
        /// </summary>
        public bool Lock
        {
            get
            {
                return IsBusy || (isLock && (DateTime.Now - lastUseDateTime).Milliseconds < 3000);
            }
            set
            {
                isLock = value;
                if (value)
                {
                    lastUseDateTime = DateTime.Now;
                }
            }
        }
    }
}
